﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.equipment;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.officer;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Tools;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.officer
{
    partial class SangoOfficerHandler
    {
        public officer_fire officer_fire(string primarycode)
        {
            string userCode = SangoManager.Authentication();
            officer_fire response = ReflectionManager.CreatePojo<officer_fire>();
            response.Succeed = false;

            List<Entities.Usr_Officer> officers = OfficerDao.getUserInjobOfficers(userCode);

            Entities.Usr_Officer officer = OfficerDao.getUserOfficer(officers, primarycode);

            // 在野 则开除失败

            if (!SettingHelper.BooleanValue(officer.Hired))
            {
                response.Message = "武将已经在野";
                return response;
            }

            // 只有一位在职

            if (officers.Count == 1)
            {
                response.Message = "仅有一位武将，无法下野";
                return response;
            }

            if (SettingHelper.BooleanValue(SysOfficerHelper.searchBySerialcode(officer.Officerserialcode).Ischief))
            {
                response.Message = "主角武将无法下野";
                return response;
            }

            Entities.Usr_Building officerBuilding = UsrBuildingDao.getUserBuilding(userCode, CtiBuildingTypecodeCollection.TRAIN);


            // 卸装备

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);
            List<Entities.Usr_Equipment> equipments = EquipmentDao.getOfficerEqms(userCode, officer.Primarycode);
            List<Entities.Usr_Equipmentextra> extras = EquipmentDao.getUserEqmExtras(userCode);

            int officerEquipmentCount = equipments.Count;
            if (property.Usedrepositorycount + officerEquipmentCount > property.Repositorycount)
            {
                response.Message = "用户仓库已经爆满";
                return response;
            }

            foreach (Entities.Usr_Equipment equipment in equipments)
            {
                OfficerDao.eqmOff(officer, equipment, property);
            }

            // 卸阵法

            List<Entities.Usr_Tacticalpos> tacticals = TacticalDao.getOfficerTacticalPositions(officer.Primarycode);
            foreach (Entities.Usr_Tacticalpos pos in tacticals)
            {
                pos.Officerprimarycode = null;
            }

            // 升级队列

            Entities.Usr_Queue queue_upgrade = UsrQueueDao.getRunningQueue(userCode, CtiQueueTypecodeCollection.OFFICER_UPGRADE, officer.Primarycode);
            if (queue_upgrade != null)
                UsrQueueDao.UsrQueueCooldown(queue_upgrade);

            // database

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Update(equipments.ToArray());

                command.Update(tacticals.ToArray());

                if (OfficerFormular.officerIsTraining(officer))
                {
                    long currentdate = GlobalTimer.Instance.ToTimeSequence();
                    long enddate = (long)officer.Upgradeenddate < currentdate ? (long)officer.Upgradeenddate : currentdate;

                    // 修改经验值
                    double increaseExp = OfficerFormular.getUpgradingExperienceByBuilding(//
                            officerBuilding.Buildinglevel, //
                            officer.Upgradestartdate, //
                            enddate, //
                            officer.Upgradespeedtypecode);

                    OfficerFormular.upgrade(officer, increaseExp, profile.Userlevel, officerBuilding.Buildinglevel);
                }
                OfficerDao.stopUpgrade(officer);

                OfficerDao.fireOfficer(officer, property);
                command.Update(officer);

                if (queue_upgrade != null)
                    command.Update(queue_upgrade);

                command.Update(property);

                command.Commit();
            });

            response.usereqms = EquipmentFacade.ToEquipments(equipments, extras);
            response.officer = OfficerFacade.ToOfficer(officer);
            response.property = UsrProfileFacade.ToProperty(property);
            response.Succeed = true;
            return response;
        }

    }

    public interface officer_fire : ISangoResponse
    {
        List<usr_equipment> usereqms { get;set;}

        player_property property { get;set;}

        player_officer officer { get;set;}
    }
}
